package personal.wang.producer;

import org.apache.commons.io.FileUtils;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @className: MyCallback
 * @Description:
 * @Author: wangyifei
 * @Date: 2022/7/1 10:06
 */
public class MyCallback implements Callback {
    private static Logger logger = LoggerFactory.getLogger(MyCallback.class);
    private String record;
    private MyCallback myself ;

    public void setMyself(MyCallback myself) {
        this.myself = myself;
    }

    public String getRecord() {
        return record;
    }

    public void setRecord(String record) {
        this.record = record;
    }

    @Override
    public void onCompletion(RecordMetadata metadata, Exception exception) {
        if (!Objects.isNull(exception)) {
            try {
                FileUtils.write(new File("D:/error.txt"), this.record.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        logger.info("metadata: {}", metadata);
    }
}